import {reactive} from './reactive'
import {isObject} from '../utils'
import { track, trigger } from './effect'

const get = createGetter()
const set = createSetter()

function createGetter() {
  return (target, property, receiver) => {
    const res = Reflect.get(target, property, receiver)
    // 触发依赖收集
    track(target, property)
    // 判断res是否是对象，如果是，则它的也要触发get set
    if(isObject(res)) {
      return reactive(res)
    }
    return res
  }
}
function createSetter () {
  return (target, property, value, receiver) => {
    const res = Reflect.set(target, property, value, receiver)
    // 触发依赖更新
    trigger(target, property)
    return res
  }
}
export const mutableHandlers = {
  get,
  set
}
